package com.hengshan.domain.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.hengshan.service.ValidGroup;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.*;
import java.util.Date;
import java.util.List;

/**
 * 用户表(User)表实体类
 *
 * @author muxijun
 * @since 2023-12-07 18:41:31
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@TableName("user")
public class User {

    //ID
    @TableId(value = "id", type = IdType.AUTO)
    @NotNull(message = "id不能为空", groups = {ValidGroup.Update.class})
    private Long id;

    //用户名
    @ExcelProperty("名称")
    @NotBlank(message = "名称不能为空")
    @Size(max = 20, message = "姓名长度不超过{max}个字符")
    private String username;

    //密码
    @NotBlank(message = "名称不能为空", groups = {ValidGroup.Insert.class})
    @Length(min = 6, max = 12, message = "密码长度为{min}-{max}位", groups = {ValidGroup.Insert.class})
    private String password;

    //头像
    private String avatar;

    //邮箱
    @ExcelProperty("邮箱")
    @Email(message = "邮箱格式不正确")
    private String email;

    //性别：0女 1男
    @ExcelProperty("性别")
    private String sex;

    //手机号码
    @ExcelProperty("手机号码")
    @Pattern(regexp = "^1[3-9]\\d{9}", message = "手机号码格式不正确")
    private String phone;

    //用户类型：0普通用户 1管理员
    private String type;

    //账号状态：0停用 1正常
    private String status;

    //创建时间
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    //更新时间
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    @TableField(exist = false)
    private List<Role> roles;

    @TableField(exist = false)
    private Integer[] roleIds;

    public boolean isAdmin() {
        return null != this.id && 1L == this.id;
    }
}

